package ezonsport.android.ui.stepSystem.goalManage;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import ezonsport.android.R;
import ezonsport.android.application.PreferenceKeys;
import ezonsport.android.data.DataHelper;

public class GoalsSettings extends OrmLiteBaseActivity<DataHelper>{ 
	
	private static final String TAG = "GoalsSettings";
	
	private ListView mListView ;
	private TextView mTextGoals;
	
	private String mGoaltextList[];
	private String mGoalValueList[];
	private List<Map<String, Object>> mListItems;
	private GoalAdapter mListAdapter;
	//Custom Dialog
	private View mTextEntryView;
	private EditText mCustomEdit;
	private AlertDialog mCustomDialog;
	
	private Calendar mCalendar;
	
	//Controller and Model
	private GoalManageController mGoalController;
	private GoalManageViewModel mGoalModel;
	private Map<Date, Integer> mGoalMap;
	private static int mStepGoal;
	
	//Custom goal setting
	private ViewSwitcher mCustomViewSwitcher;
	private TextView m_textGoalsCustom;
	
	//fixed goal setting
	private ViewSwitcher mFixedViewSwitcher[];
	
	//SharedPreferences
	private SharedPreferences mSharedPre;
	private Editor mEditor;
	
	/** Called when the activity is first created. */
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.goalsettings);
        
        mTextGoals = (TextView)findViewById(R.id.goalsetting_tx_currentgoal);
        
        mListView = (ListView)findViewById(R.id.goalsetting_list_choice);
        mGoaltextList = getResources().getStringArray(R.array.golas_list);
        mGoalValueList = getResources().getStringArray(R.array.golasvalue_list);
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.goalsetting_listitem_choice,
        		R.id.goalsetting_tx_fixed_goalchoice, mGoaltextList); 
        //mListItems = getListItem();
        //mListAdapter = new GoalAdapter(this, mListItems);
        mListView.setAdapter(adapter);
      
        mCalendar = Calendar.getInstance();
        mGoalController = new GoalManageController(getHelper());
        //mGoalMap = mGoalModel.getStepGoalMap();
        mTextGoals.setText(String.valueOf(mGoalController.getLastestGoal())+"\n");
        
        //自定义目标ViewSwitch
        //mCustomViewSwitcher = (ViewSwitcher)findViewById(R.id.goalsetting_viewswitcher_custom);
        //mCustomViewSwitcher.setDisplayedChild(0);
        
        //自定义目标TextView
        //m_textGoalsCustom = (TextView)findViewById(R.id.goalsetting_tx_custom_goalchoice);
        /*m_textGoalsCustom.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick (View v){
        		changeViewSwitcher(mGoaltextList.length);
        		mCustomDialog.show();
        	}
        });*/
        
        LayoutInflater factory = LayoutInflater.from(GoalsSettings.this);
        mTextEntryView = factory.inflate(R.layout.alert_dialog_text, null);
        mCustomEdit = (EditText)mTextEntryView.findViewById(R.id.goalsetting_edit_goalstep);
        mCustomDialog = new AlertDialog.Builder(GoalsSettings.this)
	        //.setIcon(R.drawable.alert_dialog_icon)
	        .setTitle(R.string.dialog_title_goals_custom)
	        .setView(mTextEntryView)
	        .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
	            @Override
				public void onClick(DialogInterface dialog, int whichButton) {
	
	                 //User clicked OK so do some stuff 
	            	mStepGoal = Integer.valueOf(mCustomEdit.getText().toString());
	            }
	        })
	        .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
	            @Override
				public void onClick(DialogInterface dialog, int whichButton) {
	
	                //User clicked cancel so do some stuff 
	            	dialog.cancel();
	            }
	        })
	        .create();
        
        //固定目标选择组件
        //initListView();
        
        mListView.setOnItemClickListener(new OnItemClickListener(){
	    	
        	//当前点击项显示选中图片，其他项不显示
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
	    		Log.v(TAG, "onItemClick,"+"Position:" + position + "ID:" + id);
				/*for(int i=0;i<parent.getCount();i++){
					View v = mListView.getChildAt(i); 
					ImageView image = (ImageView)v.findViewById(R.id.goalsetting_image_choose);
					image.setVisibility(View.INVISIBLE);
				} */
	    		
				/*ImageView image = (ImageView)view.findViewById(R.id.goalsetting_image_choose);			
				image.setVisibility(View.VISIBLE);*/
	    		
	    		//changeViewSwitcher(position);
	    		
	    		
	    		if(position == mGoaltextList.length-1)
	    			mCustomDialog.show();
	    		else
	    			mStepGoal = Integer.valueOf(mGoalValueList[position]);	   		
	    		for(int i=0;i<parent.getCount();i++){
					View v = mListView.getChildAt(i); 
					ViewSwitcher switcher = (ViewSwitcher)v.findViewById(R.id.goalsetting_viewswitcher_fixed);
					switcher.setDisplayedChild(0);
				}
	    		ViewSwitcher switcher = (ViewSwitcher)view.findViewById(R.id.goalsetting_viewswitcher_fixed);
	    		switcher.setDisplayedChild(1);	
				
	    	}
	    }); 
        mSharedPre = getSharedPreferences(PreferenceKeys.SHAREPREFERENCENAME, MODE_PRIVATE);
        mEditor = mSharedPre.edit();
    }
    @Override
    protected void onResume(){
    	super.onResume();
    	Log.d(TAG, "On Resume");
    	mTextGoals.setText(String.valueOf(mGoalController.getLastestGoal())+"\n");
    }
    protected void onPause(){
    	Log.v(TAG, "On Pause" + String.valueOf(mStepGoal));
    	mGoalController.setLastestGoal(mStepGoal);
    	//mEditor.putString(PreferenceKeys.USER_STEP_GOAL, String.valueOf(mStepGoal));
    	super.onPause();
    	
    }
    /*private void initListView(){
    	for(int i=0;i<mGoaltextList.length;i++){
			View v = mListView.getChildAt(i); 
			Log.v(TAG, String.valueOf(v==null?0:1));
			mFixedViewSwitcher[i] = (ViewSwitcher)v.findViewById(R.id.goalsetting_viewswitcher_fixed);
			mFixedViewSwitcher[i].setDisplayedChild(0);
    	}
    }
    private void changeViewSwitcher(int position){
    	int length = mGoaltextList.length;
    	//选中的是ListView中选项
    	if(position < length){
    		for(int i=0; i<length; i++)
    			mFixedViewSwitcher[i].setDisplayedChild(0);
    		mFixedViewSwitcher[position].setDisplayedChild(1);
    		mCustomViewSwitcher.setDisplayedChild(0);
    	}else{//选中的为自定义目标的TextView
    		for(int i=0; i<length; i++)
    			mFixedViewSwitcher[i].setDisplayedChild(0);
    		mCustomViewSwitcher.setDisplayedChild(1);
    	}
    }*/

    
    
    
    /*public List<Map<String, Object>> getListItem(){
    	List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
    	for(int i=0; i<goaltextList.length; i++){
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put(SystemConstant.GoalsSetting.GOALTEXT, goaltextList[i]);
    		listItems.add(map);
    	}
    	return listItems;
    }*/
    
       /*  m_list.setOnItemSelectedListener(new OnItemSelectedListener(){
        	@Override
        	public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
        		for(int i=0;i<parent.getCount();i++){
        			View v = m_list.getChildAt(i); 
        			RadioButton radio = (RadioButton)v.findViewById(R.id.goalsetting_radiobutton);
        			radio.setChecked(false);
        		}  
        		RadioButton radio = (RadioButton)view.findViewById(R.id.goalsetting_radiobutton);
        		radio.setChecked(true);
        	}
        	@Override
        	public void onNothingSelected (AdapterView<?> parent){
        		
        	}
        });*/
        
       /* m_textGoalsCustom = (TextView)findViewById(R.id.tx_goals_custom);
        m_textGoalsCustom.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick (View v){
        		LayoutInflater factory = LayoutInflater.from(GoalsSettings.this);
                final View textEntryView = factory.inflate(R.layout.alert_dialog_text, null);
                new AlertDialog.Builder(GoalsSettings.this)
                    //.setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(R.string.dialog_title_goals_custom)
                    .setView(textEntryView)
                    .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                        @Override
						public void onClick(DialogInterface dialog, int whichButton) {
        
                             User clicked OK so do some stuff 
                        	
                        }
                    })
                    .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                        @Override
						public void onClick(DialogInterface dialog, int whichButton) {

                             User clicked cancel so do some stuff 
                        	dialog.cancel();
                        }
                    })
                    .create().show();
        	}
        });
        */
	
    
}
